﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Net;
using System.IO;
using System.Xml.Linq;
using ShopB2C.Helper;

namespace ShopB2C.Controllers.Manager
{
    public class TaobaoController : BaseController
    {
        #region 基础设置

        private const string _taobao = "http://gw.api.tbsandbox.com/router/rest";
        private const string _appKey = "12078916";
        private const string _appSecret = "d00ceb6226ccfd9e287635ca378af15f";
        private const string _formatXml = "xml";
        private static Dictionary<string , string> _setting;

        #endregion

        /// <summary>
        /// 生成签名
        /// </summary>
        private string Sign(Dictionary<string , string> parameter)
        {
            string sign = string.Empty;

            foreach (var item in _setting)
            {
                sign += string.Format("{0}{1}{0}" , _appSecret , sign);
            }

            foreach (var item in parameter)
            {
                sign += string.Format("{0}{1}" , item.Key , item.Value);
            }
            sign += string.Format("{0}{1}{0}" , _appSecret , sign);
            sign = SecurityHelper.MD5Hash(sign).ToUpper();
            return sign;
        }
        /// <summary>
        /// 生成查询字符串
        /// </summary>
        private string QueryString(Dictionary<string , string> parameter)
        {
            string queryString = string.Empty;
            foreach (var item in _setting)
            {
                queryString += string.Format("&{0}={1}" , item.Key , item.Value);
            }

            foreach (var item in parameter)
            {
                queryString += string.Format("&{0}={1}" , item.Key , item.Value);
            }
            return queryString;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public TaobaoController()
        {
            _setting = new Dictionary<string , string>();
            _setting.Add("sign_method" , "md5");
            _setting.Add("timestamp" , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            _setting.Add("v" , "2.0");
            _setting.Add("format" , _formatXml);
            _setting.Add("app_key" , _appKey);
        }
        /// <summary>
        /// 同步淘宝分类
        /// </summary>
        public ActionResult ClassifySyncHronization(int classId = 0)
        {
            Dictionary<string , string> parameter = new Dictionary<string , string>();

            parameter.Add("method" , "taobao.itemcats.get");
            parameter.Add("parent_cid" , classId.ToString());
            parameter.Add("fields" , "cid,parent_cid,name,is_parent,status,sort_order");

            string url = string.Format("{0}?{1}&sign={2}" , _taobao , QueryString(parameter) , Sign(parameter));

            XDocument xD = XDocument.Load(url);
            
            Response.Write(xD.ToString());

            return new EmptyResult();
        }

    }
}
